package cc.rengu.igas.bomp.common.dao.impl;


import cc.rengu.igas.bomp.common.dao.BompMchntAcctSumMapper;
import cc.rengu.igas.bomp.common.entity.MchntAcctDetail;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangxuran on 2020/03/30
 */
public class BompMchntAcctSumMapperImpl implements BompMchntAcctSumMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);

    @Override
    public List<MchntAcctDetail> selecBompMchntAcctSumByMchntNoAndLimDate(String instId, String mchntNo, String startDate, String endDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selecBompMchntAcctDetailByMchntNoAndLimDate");
        MchntAcctDetail mchntAcctDetail = new MchntAcctDetail();
        mchntAcctDetail.setInstId(instId);
        mchntAcctDetail.setMchntNo(mchntNo);
        mchntAcctDetail.setBatchDate(startDate);
        mchntAcctDetail.setRemark5(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntAcctDetail, MchntAcctDetail.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询商户最近连续30天交易失败");
            return new ArrayList<MchntAcctDetail>();
        }
        return objectList.stream().map(item -> (MchntAcctDetail) item).collect(Collectors.toList());
    }
}
